define(['zepto', 'butterfly/view','../../bsl',
    'text!./orientation_level.html'
], function($, View, bsl, orientation_level) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap .openDoc" : "openDoc",
            "tap #back" : "back",

      
        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等           
            this.bslReady();

        },
              bslReady:  function (){
            // 锁定只能竖屏显示
            bsl.screen.lockOrientation( "portrait-primary" );
        },
        // if(window.bsl){
        //     bslReady();
        // }else{
        //     document.addEventListener("bslready",bslReady,false);
        // }
        // 解锁并关闭
         // _back:window.back,
        unlockback:function (){
            bsl.screen.unlockOrientation();
        },
        // window.back=unlockback;

        var t, xh, xv;
        var R = 100;
        update:function (h, v) {
            var ch = 0, cv = 0;
            if (h > 90) {
                h = 180 - h;
            } else if (h < -90) {
                h = -180 - h;
            }
            if (v > 90) {
                v = 180 - v;
            } else if (v < -90) {
                v = -180 - v;
            }
            var r = Math.max(Math.abs(h), Math.abs(v)) / 90;
            var range = Math.round(R * r);
            if (h == 0) {
                cv = range * v / 90;
            } else if (v == 0) {
                ch = range * h / 90;
            } else {
                var a = Math.atan2(v, h);
                ch = range * Math.cos(a);
                cv = range * Math.sin(a);
            }
            ch = Math.round(ch);
            cv = Math.round(cv);

            t.style.webkitTransform = "translate(" + ch + "px," + cv + "px)";

            h = h.toFixed(1);
            v = v.toFixed(1);
            xh.innerHTML = "" + (-h);
            xv.innerHTML = "" + (-v);
        },
        document.addEventListener("bslready", function() {
            t = document.getElementById("bubble");
            R = Math.floor(t.offsetWidth * 130 / 320);
            xh = document.getElementById("h");
            xv = document.getElementById("v");

            bsl.orientation.watchOrientation(function(r) {
                update(-r.gamma, -r.beta);
            }, function(e) {
                // error
            }, {
                frequency : 100
            });
        }, false);
        // Adjective select.
        document.onselectstart = function() {
            return false;
        },
          back : function(e){
            this.preateBack();
            window.history.go(-1);
        },
        openDoc:function(){
             window.butterfly.navigate('/doc/events.html', {
                trigger: true
            });


        },
      
             
            });
    return view;
});
